home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58606 / 58606.xpi / chrome / translator.jar / content / translatorUI.js < prev   
Text File  |  2010-02-07  |  8KB  |  242 lines

  1.  
  2. (function(namespace, $)
  3. {
  4.     namespace.TranslatorUI = function()
  5.     {
  6.         // empty constructor
  7.     };
  8.     
  9.     namespace.TranslatorUI.prototype = {
  10.         init: function()
  11.         {
  12.             // set status panel listeners
  13.             $('#translator-status-icon, #translator-status-label').bind('click.translator', function(e) {
  14.                 if(e.button === 0) {
  15.                     $(document).trigger('translatorStateSwitched.translator');
  16.                 }
  17.             }.bind(this));
  18.             
  19.             // set toolbar button listeners
  20.             $('#translator-toolbarbutton')
  21.                 .bind('command.translator', function(e) {
  22.                     $(document).trigger('translatorStateSwitched.translator');
  23.                 }.bind(this))
  24.                 .bind('popupshowing.translator', function(e) {
  25.                     if(e.target == $('#translator-toolbarbutton-menu').get(0)) {
  26.                         $('#translator-context-menu').contents().appendTo(e.target);
  27.                     }
  28.                 }.bind(this))
  29.                 .bind('popuphiding.translator', function(e) {
  30.                     if(e.target == $('#translator-toolbarbutton-menu').get(0)) {
  31.                         $(e.target).contents().appendTo('#translator-context-menu');
  32.                     }
  33.                 }.bind(this));
  34.             
  35.             // set translator menu listeners
  36.             $('#translator-context-menuitem-enable, #translator-context-menuitem-disable').bind('command.translator', function(e) {
  37.                 $(document).trigger('translatorStateSwitched.translator');
  38.                 
  39.                 e.stopPropagation();
  40.             }.bind(this));
  41.             
  42.             $('#translator-context-menuitem-translate').bind('command.translator', function(e) {
  43.                 $(document).trigger('translatorTranslateSelection.translator');
  44.                 
  45.                 e.stopPropagation();
  46.             }.bind(this));
  47.             
  48.             $('#translator-context-languages-menu').bind('command.translator', this.languageChangedHandler.bind(this));
  49.             
  50.             // set context menu popup listeners
  51.             $('#translator-content-area-menu')
  52.                 .bind('popupshowing.translator', function(e) {
  53.                     if(e.target == $('#translator-content-area-menupopup').get(0)) {
  54.                         $('#translator-context-menuitem-translate').after(this.getMenuPlaceholder()).appendTo(e.target);
  55.                         $('<menuseparator />').appendTo(e.target);
  56.                         $('#translator-context-languages').after(this.getMenuPlaceholder()).appendTo(e.target);
  57.                     }
  58.                 }.bind(this))
  59.                 .bind('popuphiding.translator', function(e) {
  60.                     if(e.target == $('#translator-content-area-menupopup').get(0)) {
  61.                         var placeholders = $('#translator-context-menu menuitem[placeholder="true"]');
  62.                         
  63.                         $('#translator-context-menuitem-translate').insertAfter($(placeholders[0]));
  64.                         $('#translator-context-languages').insertAfter($(placeholders[1]));
  65.                         
  66.                         $(placeholders).remove();
  67.                         $(e.target).empty();
  68.                     }
  69.                 }.bind(this));
  70.             
  71.             // preferences listener
  72.             $('#translator-context-menuitem-preferences').bind('command.translator', function(e) {
  73.                 this.openPreferences();
  74.                 
  75.                 e.stopPropagation();
  76.             }.bind(this));
  77.             
  78.             // show in context menu listener
  79.             $('#translator-context-menuitem-options-context').bind('command.translator', function(e) {
  80.                 $(document).trigger('translatorShowContextMenuSwitched.translator', $(e.target).boolean('checked'));
  81.                 
  82.                 e.stopPropagation();
  83.             }.bind(this));
  84.             
  85.             // show in toolbar listener
  86.             $('#translator-context-menuitem-options-toolbar').bind('command.translator', function(e) {
  87.                 $(document).trigger('translatorShowToolbarSwitched.translator', $(e.target).boolean('checked'));
  88.                 
  89.                 e.stopPropagation();
  90.             }.bind(this));
  91.             
  92.             // show in status bar listener
  93.             $('#translator-context-menuitem-options-statusbar').bind('command.translator', function(e) {
  94.                 $(document).trigger('translatorShowStatusBarSwitched.translator', $(e.target).boolean('checked'));
  95.                 
  96.                 e.stopPropagation();
  97.             }.bind(this));
  98.         },
  99.         
  100.         showMessage: function(message)
  101.         {
  102.             
  103.         },
  104.         
  105.         showDefaultState: function()
  106.         {
  107.             $('#translator-status-bar, #translator-toolbarbutton').attr('state', 'enabled');
  108.             $('#translator-context-menuitem-enable').boolean('hidden', true);
  109.             $('#translator-context-menuitem-disable').boolean('hidden', false);
  110.         },
  111.         
  112.         showErrorState: function()
  113.         {
  114.             $('#translator-status-bar, #translator-toolbarbutton').attr('state', 'error');
  115.             $('#translator-context-menuitem-enable').boolean('hidden', true);
  116.             $('#translator-context-menuitem-disable').boolean('hidden', false);
  117.         },
  118.         
  119.         showDisabledState: function()
  120.         {
  121.             $('#translator-status-bar, #translator-toolbarbutton').attr('state', 'disabled');
  122.             $('#translator-context-menuitem-enable').boolean('hidden', false);
  123.             $('#translator-context-menuitem-disable').boolean('hidden', true);
  124.         },
  125.         
  126.         showContextMenu: function(state)
  127.         {
  128.             $('#translator-content-area-menu').boolean('hidden', (!state));
  129.             $('#translator-context-menuitem-options-context').boolean('checked', state);
  130.         },
  131.         
  132.         showToolbar: function(state)
  133.         {
  134.             var navBar = $('#nav-bar')[0];
  135.             var currentSet = navBar.currentSet;
  136.             
  137.             // add toolbar button only if it is not there yet
  138.             if(currentSet.search(/translator-toolbarbutton-container/ig) == -1) {
  139.                 currentSet = currentSet.replace(/urlbar-container/i, 'translator-toolbarbutton-container,urlbar-container');
  140.                 
  141.                 $(navBar).attr('currentset', currentSet);
  142.                 
  143.                 // overwrite current set
  144.                 navBar.currentSet = currentSet;
  145.                 
  146.                 // persist navbar
  147.                 document.persist('nav-bar', 'currentset');
  148.             }
  149.             
  150.             // toolbar should be shown after it was added to the panel
  151.             // it does NOT work otherwise
  152.             $('#translator-toolbarbutton').boolean('hidden', (!state));
  153.             $('#translator-context-menuitem-options-toolbar').boolean('checked', state);
  154.         },
  155.         
  156.         showStatusBar: function(state)
  157.         {
  158.             $('#translator-status-bar').boolean('hidden', (!state));
  159.             $('#translator-context-menuitem-options-statusbar').boolean('checked', state);
  160.         },
  161.         
  162.         showStatusBarIcon: function(state)
  163.         {
  164.             $('#translator-status-icon').boolean('hidden', (!state));
  165.         },
  166.         
  167.         showStatusBarLabel: function(state)
  168.         {
  169.             $('#translator-status-label').boolean('hidden', (!state));
  170.         },
  171.         
  172.         openPreferences: function()
  173.         {
  174.             window.openDialog('chrome://translator/content/preferences.xul', 'preferences', 'centerscreen,chrome');
  175.         },
  176.         
  177.         enableHotKey: function()
  178.         {
  179.             // set keyboard shortcuts listeners
  180.             $('#translator-translate-key').unbind('command.translator').bind('command.translator', function(e) {
  181.                 $(document).trigger('translatorTranslateSelection.translator');
  182.                 
  183.                 e.stopPropagation();
  184.             });
  185.         },
  186.         
  187.         disableHotKey: function()
  188.         {
  189.             $('#translator-translate-key').unbind('command.translator');
  190.         },
  191.         
  192.         setHotKey: function(modifiersString, keyString)
  193.         {
  194.             $('#translator-translate-key')
  195.                 .attr('modifiers', modifiersString)
  196.                 .attr('key', keyString);
  197.         },
  198.         
  199.         setLanguage: function(code, name)
  200.         {
  201.             // set status label text
  202.             $('#translator-status-label').val(name);
  203.             
  204.             // set language in menu
  205.             $('#translator-context-languages-menu').children().each(function(i, menuitem) {
  206.                 if(code.toLowerCase() == $(menuitem).attr('code').toLowerCase()) {
  207.                     $(menuitem).boolean('checked', true);
  208.                 }
  209.             }.bind(this));
  210.         },
  211.         
  212.         loadLanguages: function(languages)
  213.         {
  214.             var $menu = $('#translator-context-languages-menu').eq(0).empty();
  215.             
  216.             $.each(languages, function(code, name) {
  217.                 var $menuitem = $(document.createElement('menuitem'));
  218.                 
  219.                 $menuitem.attr('label', name);
  220.                 $menuitem.attr('code', code);
  221.                 $menuitem.attr('type', 'radio');
  222.                 $menuitem.attr('name', 'language');
  223.                 
  224.                 $menuitem.appendTo($menu);
  225.             }.bind(this));
  226.         },
  227.         
  228.         getMenuPlaceholder: function()
  229.         {
  230.             return $('#translator-menuitem-placeholder').clone().removeAttr('id');
  231.         },
  232.         
  233.         languageChangedHandler: function(e)
  234.         {
  235.             var languageCode = $(e.originalTarget).attr('code');
  236.             
  237.             $(document).trigger('translatorLanguageChanged.translator', [languageCode]);
  238.             
  239.             e.stopPropagation();
  240.         }
  241.     };
  242. })(com.igorgladkov.translator, translatorJQuery);